//
//  BIDShowPhotoView.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/5.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDShowPhotoView.h"
#import "AppDelegate+StaticMethod.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface BIDShowPhotoView()
{
    NSInteger _count;
    NSMutableArray *_imgArr;
    NSArray *_imgPathArr;
}
@end

@implementation BIDShowPhotoView
@synthesize delegate;

+ (CGFloat)heightForShowPhotoViewWithImgCount:(NSInteger)count andViewWidth:(CGFloat)width
{
    //
    CGSize itemSize = CGSizeMake(80, 80);
    CGFloat topSpacing = 0.f;
    CGFloat leftSpacing = 10.f;
    CGFloat rightSpacing = 10.f;
    CGFloat bottomSpacing = 0.f;
    CGFloat lineSpacing = 8.f;
    CGFloat minimumItemSpacing = 10.f;
    CGFloat itemSpacing = 0.f;
    //
    int countPerRow = (width+minimumItemSpacing-leftSpacing-rightSpacing)/(itemSize.width+minimumItemSpacing);
    itemSpacing = (width-leftSpacing-rightSpacing-countPerRow*itemSize.width)/(countPerRow-1);
    //总行数
    NSInteger totalRows = count<=countPerRow?1:(count+countPerRow-1)/countPerRow;
    CGFloat height = topSpacing + totalRows*itemSize.height + (totalRows-1)*lineSpacing + bottomSpacing;
    return height;
}

- (id)initWithImgPathArr:(NSArray *)arr andFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        _count = arr.count;
        _imgArr = [NSMutableArray array];
        _imgPathArr = [[NSArray alloc] initWithArray:arr];
        //
    }
    return self;
}

- (void)adjustLayoutWithImgPathArr:(NSArray *)arr
{
    _count = arr.count;
    _imgArr = [NSMutableArray array];
    _imgPathArr = [[NSArray alloc] initWithArray:arr];
    //
    CGSize itemSize = CGSizeMake(80, 80);
    CGFloat topSpacing = 0.f;
    CGFloat leftSpacing = 10.f;
    CGFloat rightSpacing = 10.f;
    CGFloat bottomSpacing = 0.f;
    CGFloat lineSpacing = 8.f;
    CGFloat minimumItemSpacing = 10.f;
    CGFloat itemSpacing = 0.f;
    //
    int countPerRow = (CGRectGetWidth(self.frame)+minimumItemSpacing-leftSpacing-rightSpacing)/(itemSize.width+minimumItemSpacing);
    itemSpacing = (CGRectGetWidth(self.frame)-leftSpacing-rightSpacing-countPerRow*itemSize.width)/(countPerRow-1);
    //总行数
    NSInteger totalRows = _count<=countPerRow?1:(_count+countPerRow-1)/countPerRow;
    int i=0;
    while(i<_count)
    {
        CGFloat posX = leftSpacing;
        CGFloat posY = topSpacing;
        if(i!=0 && fmodf(i, countPerRow)==0)
        {
            //换行
            posX = leftSpacing;
        }
        else
        {
            posX = leftSpacing + fmodf(i, countPerRow)*itemSize.width + fmodf(i, countPerRow)*itemSpacing;
        }
        posY = topSpacing + i/countPerRow*itemSize.height + i/countPerRow*lineSpacing;
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(posX, posY, itemSize.width, itemSize.height)];
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
        [imgView addGestureRecognizer:tapGR];
        imgView.userInteractionEnabled = YES;
        imgView.tag = i;
        NSString *strImgPath = _imgPathArr[i];
        imgView.layer.borderWidth = 0.5f;
        imgView.layer.borderColor = [UIColor blackColor].CGColor;
        [imgView setImageWithURL:[NSURL URLWithString:strImgPath] placeholderImage:[UIImage imageNamed:@"pic_placeholder.png"]];
        [self addSubview:imgView];
        i++;
    }
    CGRect frame = self.frame;
    frame.size.height = topSpacing + totalRows*itemSize.height + (totalRows-1)*lineSpacing + bottomSpacing;
    self.frame = frame;
}

- (void)tapGestureHandler:(UITapGestureRecognizer*)tapGR
{
    NSArray *arr = self.subviews;
    [_imgArr removeAllObjects];
    for(UIView *subView in arr)
    {
        UIImageView *imgView = (UIImageView*)subView;
        [_imgArr addObject:imgView.image];
    }
    UIView *view = tapGR.view;
    //NSNumber *indexValue = [NSNumber numberWithInteger:view.tag];
    [delegate showPhotoAtIndex:view.tag withImgArr:_imgArr];
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"showPhoto" object:nil userInfo:@{@"index":indexValue, @"imgArr":_imgArr}];
}


@end
